package com.masterclient.app.businessmodule.vipmodule.view;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.eoner.baselibrary.utils.Utils;
import com.eoner.managerlibrary.system.SystemConfigStorage;
import com.masterclient.app.R;
import com.masterclient.app.businessmodule.vipmodule.activity.InComeActivity;
import com.masterclient.app.businessmodule.vipmodule.activity.SaleOrderActivity;
import com.masterclient.app.businessmodule.vipmodule.activity.ServiceOrderActivity;
import com.masterclient.app.businessmodule.vipmodule.bean.DealerShop;
import com.masterclient.app.businessmodule.vipmodule.bean.Member;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 代理会员中心页面
 */
public class AgentMemberView extends LinearLayout {
    @BindView(R.id.total_tv)
    TextView totalTv;
    @BindView(R.id.tv1)
    TextView tv1;
    @BindView(R.id.unit_tv)
    TextView unitTv;
    @BindView(R.id.title_tv)
    TextView titleTv;
    @BindView(R.id.num1_tv)
    TextView num1Tv;
    @BindView(R.id.name1_tv)
    TextView name1Tv;
    @BindView(R.id.num2_tv)
    TextView num2Tv;
    @BindView(R.id.name2_tv)
    TextView name2Tv;
    @BindView(R.id.num3_tv)
    TextView num3Tv;
    @BindView(R.id.name3_tv)
    TextView name3Tv;
    @BindView(R.id.num4_tv)
    TextView num4Tv;
    @BindView(R.id.name4_tv)
    TextView name4Tv;
    private List<TextView> nums;
    private List<TextView> names;
    private String dealerProfit;
    public AgentMemberView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setOrientation(LinearLayout.VERTICAL);
        initView();
    }

    private void initView() {
        LayoutInflater.from(getContext()).inflate(R.layout.agent_member_view, this);
        ButterKnife.bind(this);
        Utils.setDin(num1Tv, getContext());
        Utils.setDin(num2Tv, getContext());
        Utils.setDin(num3Tv, getContext());
        Utils.setDin(num4Tv, getContext());
        Utils.setDin(totalTv, getContext());
        nums = new ArrayList<>();
        names = new ArrayList<>();
        nums.add(num1Tv);
        nums.add(num2Tv);
        nums.add(num3Tv);
        nums.add(num4Tv);
        names.add(name1Tv);
        names.add(name2Tv);
        names.add(name3Tv);
        names.add(name4Tv);

    }

    @OnClick({R.id.income_view, R.id.item1_v, R.id.item2_v, R.id.item3_v, R.id.item4_v, R.id.tv1})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.income_view:
                getContext().startActivity(new Intent(getContext(), InComeActivity.class));
                break;
            case R.id.item1_v:
                startSaleOrderActivity(names.get(0).getTag().toString());
                break;
            case R.id.item2_v:
                startSaleOrderActivity(names.get(1).getTag().toString());
                break;
            case R.id.item3_v:
                startSaleOrderActivity(names.get(2).getTag().toString());
                break;
            case R.id.item4_v:
                getContext().startActivity(new Intent(getContext(), ServiceOrderActivity.class));
                break;
            case R.id.tv1:
                boolean open = SystemConfigStorage.getInstance().isOpen();
                SystemConfigStorage.getInstance().setOpen(!open);
                setEye();
                break;
        }
    }

    /**
     * 跳转到订单列表
     * @param target
     */
    private void startSaleOrderActivity(String target){
        Intent intent = new Intent(getContext(),SaleOrderActivity.class);
        intent.putExtra("target",target);
        getContext().startActivity(intent);
    }

    /**
     * 设置零售台信息
     * @param dealerShopBean
     */
    public void setDealerShop(Member.DealerShopBean dealerShopBean,String profit) {
        if(null != profit) {
            dealerProfit = profit;
            totalTv.setText(dealerProfit);
        }
        List<DealerShop> list = dealerShopBean.getList();
        if(null != list){
            for(int i=0;i<list.size();i++){
                DealerShop dealerShop = list.get(i);
                nums.get(i).setText(dealerShop.getVal());
                names.get(i).setText(dealerShop.getName());
                names.get(i).setTag(dealerShop.getTarget());
            }
        }
        titleTv.setText(dealerShopBean.getTitle());
        setEye();
    }

    /**
     * 设置眼睛打开或闭合
     */
    private void setEye() {
        if (SystemConfigStorage.getInstance().isOpen()) {
            tv1.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.icon_open_eyes, 0);
            // totalTv.setInputType(InputType.TYPE_CLASS_TEXT);
            //  totalTv.setTypeface(Typeface.DEFAULT_BOLD);
            //  unitTv.setInputType(InputType.TYPE_CLASS_TEXT);
            totalTv.setTextSize(TypedValue.COMPLEX_UNIT_SP,23);
            totalTv.setText(dealerProfit);
            unitTv.setVisibility(VISIBLE);
        } else {
            tv1.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.icon_close_eyes, 0);
            //totalTv.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
            // unitTv.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
            totalTv.setTextSize(TypedValue.COMPLEX_UNIT_SP,15);
            totalTv.setText("*** ***");
            unitTv.setVisibility(GONE);
        }
    }

}
